<?php

namespace Caisse\CaisseBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Caisse\CaisseBundle\Entity\Article;
use Caisse\CaisseBundle\Entity\Categorie;
use Caisse\CaisseBundle\Form\ArticleType;
use Symfony\Component\HttpFoundation\Response;

class ArticleController extends Controller
{
    
        public function listArticleAction()
    {
        $em= $this->getDoctrine()->getManager();
        $articles = $em->getRepository("CaisseBundle:Article")->findAll();
        
        return $this->render('CaisseBundle:Article:listArticle.html.twig', array(
            'articles' => $articles,
        ));
    }
           public function listArticleMobileAction()
    {
        $em = $this->getDoctrine()->getManager();
        $request=  $this->getRequest();
        $id=$request->get('id');
        $articles = $em->getRepository("CaisseBundle:Article")->getCategArticle($id);
        $resultat = array();
        $resultat['articles'] = array();
        foreach ($articles as $article){
             $tab = array();
             $tab ['id']=$article->getId();
             $tab['nomArticle'] = $article->getNomArticle();
             $tab['prix'] = $article->getPrix();
             
            $resultat['articles'][] = $tab;
            
        
        }
        return new Response(json_encode($resultat));
        
    }
    
    public function ajoutArticleAction()
    {   
        $em = $this->getDoctrine()->getEntityManager();
        $request = $this->getRequest();
        $id = $request->get('id');
        if($id != "")
        $article = $em->getRepository('CaisseBundle:Article')->find($id);
        else
            $article = new Article();
           
        
        $form = $this->createForm(new ArticleType(), $article);
        
        
        if ($request->isMethod('POST')) {
            $form->bind($request);
             $article->upload();
            if ($form->isValid()) {
                $article = $form->getData();
                
                
                $repository = $em->getRepository('CaisseBundle:Article');
                $em->persist($article);
                $em->flush();
                $articles = $repository->findAll();
                 return $this->render('CaisseBundle:Article:listArticle.html.twig', array('articles' => $articles));
            }
        }
        return $this->render('CaisseBundle:Article:ajoutArticle.html.twig', array('form' => $form->createView(), 'id' => $id));

  
        
    }
    public function modifierArticleAction() {
        $em = $this->getDoctrine()->getManager();
        $request = $this->getRequest();
        $id = $request->get('id');
        $article = $em->getRepository('CaisseBundle:Article')->find($id);
        $form = $this->createForm(new ArticleType(),$article);
        print "xxcxcxc";
        if ($request->isMethod('post')) {
            $article->bind($request);
            if ($form->isValid()) {
                $article = $form->getData();
                $em->persist($article);
                $em->flush();
                $url = $this->generateUrl("caisse_list_article");
                return $this->redirect($url);
            }
        }
        return $this->render('CaisseBundle:Article:ajoutArticle.html.twig', array('form' => $form->createView(), 'id' => $id));

    }
    
    public function supprimerArticleAction() {
        $request = $this->getRequest();
        $id = $request->get('id');
        $em = $this->getDoctrine()->getManager();
        $produit = $em->getRepository('CaisseBundle:Article')->find($id);
        $em->remove($produit);
        $em->flush();
        $url = $this->generateUrl("caisse_list_article");
        return $this->redirect($url);
    }

   }
